﻿using LSFTP.model;
using System;
using System.IO;

namespace Rainbow.model
{
    public class FileInfoViewModel : ViewModelBase
    {
        public FileInfoViewModel() { }

        public FileInfoViewModel(FTPFileInfo info)
        {
            this.Name = info.Name;
            this.FullName = info.FullName;
            this.Length = info.Length;
            this.IsDirectory = info.IsDirectory;
            this.LastAccessTime = info.LastAccessTime;
            this.LastWriteTime = info.LastWriteTime;
            this.IsSelected = false;
        }

        public FileInfoViewModel(FileInfo info)
        {
            this.Name = info.Name;
            this.FullName = info.FullName;
            this.Length = info.Length;
            this.IsDirectory = false;
            this.LastAccessTime = info.LastAccessTime;
            this.LastWriteTime = info.LastWriteTime;
            this.IsSelected = false;
        }

        public FileInfoViewModel(DirectoryInfo info)
        {
            this.Name = info.Name;
            this.FullName = info.FullName;
            this.Length = 0;
            this.IsDirectory = true;
            this.LastAccessTime = info.LastAccessTime;
            this.LastWriteTime = info.LastWriteTime;
            this.IsSelected = false;
        }

        public FTPFileInfo ToInfo()
        {
            FTPFileInfo info = new FTPFileInfo();
            info.Name = this.Name;
            info.FullName = this.FullName;
            info.Length = this.Length;
            info.IsDirectory = this.IsDirectory;
            info.LastAccessTime = this.LastAccessTime;
            info.LastWriteTime = this.LastWriteTime;
            return info;
        }

        private string m_Name;
        public string Name
        {
            get { return m_Name; }
            set
            {
                m_Name = value;
                NotifyPropertyChanged();
            }
        }

        private string m_FullName;
        public string FullName
        {
            get { return m_FullName; }
            set
            {
                m_FullName = value;
                NotifyPropertyChanged();
            }
        }

        private long m_Length;
        public long Length
        {
            get { return m_Length; }
            set
            {
                m_Length = value;
                if(m_Length <= 0)
                {
                    LengthStr = string.Empty;
                }
                else
                {
                    double val = value / 1024;
                    if(val <= 0)
                    {
                        LengthStr = "1 KB";
                    }
                    else if(val < 1023)
                    {
                        LengthStr = (int)(val + 1) + " KB";
                    }
                    else
                    {
                        val = val / 1024.0;
                        if(val <= 0)
                        {
                            LengthStr = "1 MB";
                        }
                        else if(val < 1023)
                        {
                            LengthStr = (val).ToString("F1") + " MB";
                        }
                        else
                        {
                            val = val / 1024.0;
                            if (val <= 0)
                            {
                                LengthStr = "1 GB";
                            }
                            else if (val < 1023)
                            {
                                LengthStr = (val).ToString("F1") + " GB";
                            }
                        }
                    }
                }
                NotifyPropertyChanged();
            }
        }

        private string m_LengthStr;
        public string LengthStr
        {
            get { return m_LengthStr; }
            set
            {
                m_LengthStr = value;
                NotifyPropertyChanged();
            }
        }

        private bool m_IsDirectory;
        public bool IsDirectory
        {
            get { return m_IsDirectory; }
            set
            {
                m_IsDirectory = value;
                if(value == true)
                {
                    Type = "文件夹";
                    TypePic = "/resources/images/folder.png";
                }
                else
                {
                    Type = "文件";
                    TypePic = "/resources/images/file.png";
                }
                NotifyPropertyChanged();
            }
        }

        private DateTime m_LastAccessTime;
        public DateTime LastAccessTime
        {
            get { return m_LastAccessTime;}
            set
            {
                m_LastAccessTime = value;
                NotifyPropertyChanged();
            }
        }

        private DateTime m_LastWriteTime;
        public DateTime LastWriteTime
        {
            get { return m_LastWriteTime;}
            set
            {
                m_LastWriteTime = value;
                NotifyPropertyChanged();
            }
        }

        private string m_Type;
        public string Type
        {
            get { return m_Type; }
            set
            {
                m_Type = value;
                NotifyPropertyChanged();
            }
        }

        private string m_TypePic;
        public string TypePic
        {
            get { return m_TypePic; }
            set
            {
                m_TypePic = value;
                NotifyPropertyChanged();
            }
        }

        private bool m_IsSelected;
        public bool IsSelected
        {
            get { return m_IsSelected; }
            set
            {
                m_IsSelected = value;
                NotifyPropertyChanged();
            }
        }
    }
}
